package com.whu.waterworks.action;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.java_websocket.client.WebSocketClient;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.whu.waterworks.mina.MyWebSocketClient;
import com.whu.waterworks.model.Alert;
import com.whu.waterworks.service.AlertService;
/**
 * 
 * @author cai
 * @date   2016-3-22
 *
 * 发送报警信息给客户端
 */

public class DisposeAlert extends ActionSupport{

	private static final long serialVersionUID = 8283090235030193944L;
	private String alertType;
	private String uppervalue;
	private String lowervalue;
	private String alertContent;
	private String value;
	private Map<String, String> alertMap=new HashMap<String, String>();
	private String alertMapString="";
	private String result;
	private AlertService alertService;
	private List<Alert> alerts=new ArrayList<>();
	@Override
	public String execute() throws Exception {
		alertMap.put("alertType", alertType);
		alertMap.put("uppervalue", uppervalue);
		alertMap.put("lowervalue", lowervalue);
		alertMap.put("alertContent", alertContent);
		alertMap.put("value", value);
		System.out.println("alertMap:"+alertMap);
		
		try {
			WebSocketClient cc = new MyWebSocketClient( new URI("ws://127.0.0.1:2333")) ;

	  cc.connect();
	  while (cc.getConnection().isOpen()==false) {
		  if (cc.getConnection().isOpen()==true) {
			break;
		}
	  }

	
	  alertMapString =JSON.toJSONString(alertMap);
	  System.out.println("alertMapString:"+alertMapString);
	  //String s="1";
	  cc.send(alertMapString);
      cc.getConnection().close();
      cc.close();
      
		} catch (Exception e) {
			//System.out.println(e.toString());
			result=ERROR;
			e.printStackTrace();
			
		}
		result=SUCCESS;
		return SUCCESS;
		
	}
	
	public String initAlert() {
		
		return SUCCESS;
	}
	
	public String addAlert() {
		alerts = alertService.getAll();
		return SUCCESS;
	}
	
	public String queryAlert() {
		alerts = alertService.getAll();
		return SUCCESS;
	}
	
	public String getAlertType() {
		return alertType;
	}
	public void setAlertType(String alertType) {
		this.alertType = alertType;
	}
	public String getUppervalue() {
		return uppervalue;
	}
	public void setUppervalue(String uppervalue) {
		this.uppervalue = uppervalue;
	}
	public String getLowervalue() {
		return lowervalue;
	}
	public void setLowervalue(String lowervalue) {
		this.lowervalue = lowervalue;
	}
	public String getAlertContent() {
		return alertContent;
	}
	public void setAlertContent(String alertContent) {
		this.alertContent = alertContent;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public Map<String, String> getAlertMap() {
		return alertMap;
	}
	public void setAlertMap(Map<String, String> alertMap) {
		this.alertMap = alertMap;
	}
	public String getAlertMapString() {
		return alertMapString;
	}
	public void setAlertMapString(String alertMapString) {
		this.alertMapString = alertMapString;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}

	public List<Alert> getAlerts() {
		return alerts;
	}

	public void setAlerts(List<Alert> alerts) {
		this.alerts = alerts;
	}

	public AlertService getAlertService() {
		return alertService;
	}

	public void setAlertService(AlertService alertService) {
		this.alertService = alertService;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
	
}
